/*
 * 
 */
package com.flying.dcss.dcauthorization.action;

import java.io.PrintWriter;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.common.service.impl.MailService;
import com.flying.dcss.common.service.impl.SmsService;
import com.flying.dcss.dcauthorization.bo.impl.DcssApplicationOrder;
import com.flying.dcss.dcauthorization.bo.impl.DcssPermissionOrder;
import com.flying.dcss.dcauthorization.service.impl.AuthorizeService;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class PassOrderAction.
 */
public class PassOrderAction extends ActionSupport {

	/** The authorize service. */
	private AuthorizeService authorizeService;

	/** The mail service. */
	private MailService mailService;

	private SmsService smsService;

	/** The user service. */
	private UserService userService;

	/** The user info service. */
	private UserInfoService userInfoService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	/**
	 * Sets the authorize service.
	 * 
	 * @param authorizeService
	 *            the new authorize service
	 */
	public void setAuthorizeService(AuthorizeService authorizeService) {
		this.authorizeService = authorizeService;
	}

	/**
	 * Sets the mail service.
	 * 
	 * @param mailService
	 *            the new mail service
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String orderId = ServletActionContext.getRequest().getParameter(
				"orderId");
		DcssApplicationOrder order = authorizeService
				.getApplicationOrderById(orderId);
		if (order.getState() == 0) {

			authorizeService.passApplicationOrder(orderId);

			// Add to Permission Order
			DcssPermissionOrder permissionOrder = new DcssPermissionOrder(UUID
					.randomUUID().toString(), order.getUserId(), order
					.getDcId(), order.getApprover(), order.getDeadline(), true);

			authorizeService.addPermissionOrder(permissionOrder);

			smsService.sendPassOrderSMS(userService.getUserById(
					order.getUserId()).getPhoneNum(), userService.getUserById(
					order.getUserId()).getUsername(), userService.getUserById(
					order.getApprover()).getUsername(), " ");
			// Send email.

			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("Succeed!");
			out.flush();
			out.close();
			return null;
		} else {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("This application form already reviewed!");
			out.flush();
			out.close();
			return null;
		}

	}
}
